package com.pair.micro.common.mybatis.utils;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pair.micro.common.bootstraptable.BootstrapTable;
import com.pair.micro.common.request.MicroRequest;
import com.pair.micro.common.response.RtData;
import com.pair.micro.common.utils.MicroDateUtil;
import com.pair.micro.common.utils.PageResult;

/** MybatisPlus 工具类  没用*/
public class MybatisPlusUtil {

  public static final long DEFAULT_OFFSET = 1;
  public static final long DEFAULT_LIMIT = 10;
  public static final long MAX_SIZE = 5000;

  /**
   * 构建bootstrap-table分页对象Page
   *
   * @param bootstrapTable
   * @param <T>
   * @return
   */
  public static <T> Page<T> mpPage(BootstrapTable bootstrapTable) {
    Page tPage = new Page<T>();
    if (ObjectUtil.isNotNull(bootstrapTable.getPageNumber())) {
      tPage.setCurrent(bootstrapTable.getPageNumber());
    } else {
      tPage.setCurrent(DEFAULT_OFFSET);
    }
    if (ObjectUtil.isNotNull(bootstrapTable.getLimit())) {
      tPage.setSize(bootstrapTable.getLimit());
    } else {
      tPage.setSize(MAX_SIZE);
    }
    return tPage;
  }

  /**
   * 构建api接口分页对象Page
   *
   * @param microRequest
   * @param <T>
   * @return
   */
  public static <T> Page<T> mpPage(MicroRequest microRequest) {
    Page tPage = new Page<T>();
    if (ObjectUtil.isNotNull(microRequest.getPageNo())) {
      tPage.setCurrent(microRequest.getPageNo().intValue());
    } else {
      tPage.setCurrent(DEFAULT_OFFSET);
    }
    if (ObjectUtil.isNotNull(microRequest.getPageSize())) {
      tPage.setSize(microRequest.getPageSize().intValue());
    } else {
      tPage.setSize(DEFAULT_LIMIT);
    }
    return tPage;
  }


  //public static <T> Page<T> mpPage(MicroRequest microRequest) {
  //  Page tPage = new Page<T>();
  //  if (ObjectUtil.isNotNull(microRequest.getOffset())) {
  //    tPage.setCurrent(microRequest.getOffset().intValue());
  //  } else {
  //    tPage.setCurrent(DEFAULT_OFFSET);
  //  }
  //  if (ObjectUtil.isNotNull(microRequest.getLimit())) {
  //    tPage.setSize(microRequest.getLimit().intValue());
  //  } else {
  //    tPage.setSize(DEFAULT_LIMIT);
  //  }
  //  return tPage;
  //}


  /**
   * API回参对象转换
   *
   * @param iPage
   * @param microRequest
   * @return
   */
  public static RtData toRtData(IPage iPage, MicroRequest microRequest) {
    RtData rtData = new RtData();
    if (ObjectUtil.isNotNull(iPage)) {
      rtData.setPTotal(iPage.getTotal());
      rtData.setDts(iPage.getRecords());
      rtData.setPNum(iPage.getPages());
      rtData.setPTs(microRequest.getPTs());
    }
    return rtData;
  }


  public static RtData toRtData(PageResult iPage, MicroRequest microRequest) {
    RtData rtData = new RtData();
    if (ObjectUtil.isNotNull(iPage)) {
      rtData.setPTotal(iPage.getTotal());
      rtData.setDts(iPage.getList());
      rtData.setPNum(iPage.getPages());
      rtData.setPTs(microRequest.getPTs());
    }
    return rtData;
  }

  /**
   * @name: buildPageRquest
   * @description 构建分页参数
   * @author xubz
   * @date 2023-09-11 09:30
   * @return: T
   */
  public static <T extends MicroRequest> T buildPageRquest(T t) {
    if (ObjectUtil.isNull(t.getPageNo())) {
      t.setPageNo(DEFAULT_OFFSET);
    }

    if (ObjectUtil.isNull(t.getPageSize())) {
      t.setPageSize(DEFAULT_LIMIT);
    }

    if (ObjectUtil.isNull(t.getPTs())) {
      t.setPTs(MicroDateUtil.longNow());
    }

    if (StrUtil.isNotBlank(t.getSearch())) {
      t.setSearch(t.getSearch().trim());
    }

    return t;
  }

  //public static <T extends MicroRequest> T buildPageRquest(T t) {
  //  if (ObjectUtil.isNull(t.getOffset())) {
  //    t.setOffset(DEFAULT_OFFSET);
  //  }
  //
  //  if (ObjectUtil.isNull(t.getLimit())) {
  //    t.setLimit(DEFAULT_LIMIT);
  //  }
  //
  //  if (ObjectUtil.isNull(t.getPTs())) {
  //    t.setPTs(MicroDateUtil.longNow());
  //  }
  //
  //  if (StrUtil.isNotBlank(t.getSearch())) {
  //    t.setSearch(t.getSearch().trim());
  //  }
  //
  //  return t;
  //}

  /**
   * @name: buildPageRquest
   * @description 构建分页参数
   * @author xubz
   * @date 2023-09-11 09:30
   * @return: T
   */
  public static <T extends BootstrapTable> T buildPageRquest(T t) {
    if (ObjectUtil.isNull(t.getOffset())) {
      t.setOffset(1);
    }

    if (ObjectUtil.isNull(t.getLimit())) {
      t.setLimit(10);
    }

    if (StrUtil.isNotBlank(t.getSearch())) {
      t.setSearch(t.getSearch().trim());
    }

    return t;
  }

  public static final String regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}";

  private static boolean isDate(String dateStr) {
    return ReUtil.isMatch(regex, dateStr);
  }

  public static void main(String[] args) {
    System.out.println(ReUtil.isMatch(regex, "2020-01-04 12:00:00"));
  }
}
